# -------------进程池Pool--------------------
from multiprocessing import Process,Pool
import time

def f(i):
    time.sleep(1)
    print(i)
if __name__ == '__main__':
    pool=Pool(5)  # 不填参数默认以CPU数量运行
    for i in range(100):
        # pool.apply(func=f,args=(i,)) # 同步接口
        pool.apply_async(func=f,args=(i,))
    pool.close()
    pool.join()
    # 进程池的格式先close后join、调用顺序是固定的
    print("over all")

# -------------进程池Pool、回调函数--------------------
#     某个函数执行成功后再去执行的函数
import os
def f(i):
    time.sleep(1)
    print(i)
    print("son id",os.getpid())
    return "hello %s" %i
def bar(arg):
    print(arg)
    print("bar id",os.getpid())
if __name__ == '__main__':
    pool=Pool(5)  # 不填参数默认以CPU数量运行
    print("main id",os.getpid())
    for i in range(100):
        # pool.apply(func=f,args=(i,)) # 同步接口
        pool.apply_async(func=f,args=(i,),callback=bar)

    pool.close()
    pool.join()
    # 进程池的格式先close后join、调用顺序是固定的
    print("over all")